﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Admin_Web_test.Kit;
using Admin_Web_test.Models;
using Common.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Model;

namespace Admin_Web_test.Controllers
{
    public class HomeController : Controller
    {
        private readonly Common.DBHelp.MySqlContext _context;

        public HomeController(Common.DBHelp.MySqlContext context)
        {
            //https://www.cnblogs.com/yaopengfei/p/12194230.html   5.6
            context.ChangeTracker.QueryTrackingBehavior = Microsoft.EntityFrameworkCore.QueryTrackingBehavior.NoTracking;
            //关闭状态同步
            context.ChangeTracker.AutoDetectChangesEnabled = false;
            //开启：context.ChangeTracker.DetectChanges();
            _context = context;
        }

        public IActionResult Login()
        {
            Sysuser user = new Sysuser();
            var sys = HttpContext.Session.Get<Sysuser>(SessionKeysM.Login);
            if (sys != null && sys.Id.ToString().ToInt() > 0)
            {
                return Redirect("/Admin/Index");
            }
            return View(user);
        }

        [HttpPost]
        public JsonResult Login(Sysuser user)
        {
            var json = new JsonResultM();
            if (string.IsNullOrWhiteSpace(user.Password) || string.IsNullOrWhiteSpace(user.Name))
            { 
                json.msg = "不能为空";
                return Json(json);
            }
            var get = _context.Set<Sysuser>().Where(x=>x.Name==user.Name).FirstOrDefault();
            if (get!=null && MD5Help.Get32MD5(user.Password) == get.Password ) {
                json.msg = "成功";
                HttpContext.Session.Set(SessionKeysM.Login, get);
                json.success = true;
            }
            else {
                json.msg = "失败";
            }
            return Json(json);
        }

        [HttpPost]
        public JsonResult Register(Sysuser user,string ConPassword)
        {
            var json = new JsonResultM();
            if (user.Password != ConPassword)
            {
                json.msg = "密码不一致";
                return Json(json);
            }
            if (string.IsNullOrWhiteSpace(user.Password) || string.IsNullOrWhiteSpace(user.Name) || string.IsNullOrWhiteSpace(user.ShowName))
            {
                json.msg = "不能为空";
                return Json(json);
            }
            var u = _context.Set<Sysuser>().Where(x => x.Name == user.Name).FirstOrDefault();
            if (u != null)
            {
                json.msg = "该用户名已存在";
                return Json(json);
            }
            user.Password = MD5Help.Get32MD5(user.Password);
            _context.Set<Sysuser>().AddRange(user);
            _context.SaveChanges();
            json.msg = "注册成功";
            json.success = true;
            return Json(json);
        }

        [PowerActionFilter]
        public IActionResult LoginOut()
        {
            HttpContext.Session.Remove(SessionKeysM.Login);
            return Redirect("/Home/Login");
        }
    }
}
